/**角色视图
 * by Steve Jin
 */
//模型文件版本数据
var roleVersioninfoStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({	//proxy从哪里获取数据
		url : 'showRoleVersionInfo.action?method=showRoleVersionInfo',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({	//reader如何解析数据
		root : 'versionInfo',
		totalProperty : 'totalVersionInfoSize',
		id : 'roleversion'
	}, [ {
		name : 'groupId'
	}, {
		name : 'time'
	},{
		name : 'author'
	}, {
		name : 'versionId'
	}
	])
});
//版本表格列模型
var roleVersioninfosm=new Ext.grid.CheckboxSelectionModel ({singleSelect : false});
var versioninfoColumnModel=new Ext.grid.ColumnModel(
		[roleVersioninfosm,
		 new Ext.grid.RowNumberer(),
		 {
			header : '版本号',
			width : 100,
			dataIndex : 'versionId',
			sortable : true,
			align : 'center'
		 },   {
			 header : '修改日期',
			 width : 200,
			 dataIndex : 'time',
			 sortable : true,
             align : 'center'
		 },{
			 header : '作者',
			 width : 100,
			 dataIndex : 'author',
			 sortable : true,
			 align : 'center'
         },{
			 header : '模型种类',
			 width : 100,
			 dataIndex : 'groupId',
			 sortable : true,
			 align : 'center'
         }]
          );
//模型版本信息列表
var versioninfoList=new Ext.grid.EditorGridPanel({
	id:'roleversion',
	title: "版本历史",
	region:'center',
	ds :roleVersioninfoStore,	//绑定store
	enableColumnResize : true,
	border : true,
	//clicksToEdit:1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	bodyStyle:'width:100%',
    autoWidth:true,
    editable:true,
    sm : roleVersioninfosm,
    bbar: new Ext.PagingToolbar({//下面的分页条
        pageSize: 15,
        store: roleVersioninfoStore,
        displayInfo: true,
        displayMsg: '当前显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: "没有记录",
        lastText:"尾页",   
        nextText :"下一页",   
        prevText :"上一页",   
        firstText :"首页",   
        refreshText:"刷新页面"        
    }),
	cm : versioninfoColumnModel,
	store : roleVersioninfoStore
});

var versioninfoTabPanel = new Ext.TabPanel({
	border : true,
	region:'south',
	autoScroll:true,
	//clicksToEdit:1,
	//autoHeight:true,
    //autoWidth:true,
	height:200,
	items:[ versioninfoList,
	{
	title: "角色权限信息",
	region:'center',
	
	collapsible : true,
	border:false,
	contentEl:'rolePermissionDetail'
	}]
	,
	activeItem: 1
	});

var role = new Ext.Panel({
	id : 'role-panel',
	title : '角色视图',
	layout : 'border',
	defaults : {
		
		bodyStyle : 'padding:15px'
	},
	items : [ versioninfoTabPanel,
	          {
		region : 'center',
		border : false,
		autoScroll:true,
		contentEl : 'roleCanvas'
	} ]

});

function returnToRoleList(){
	
	  Ext.getCmp('content-panel').layout.setActiveItem('role-list-panel');
	  showRoleList();
}
var rpAmount;
var pAmount;
var permissionStore;
var perm;


var rolepermissionsStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showRolepermissions.action?method=showRolepermissions',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "Rolepermissions",
		totalProperty : "totalRolepermissions",
		id:"id"
    }, [ {
		name : 'id'
	}, {
		name : 'permissionid'
	},{
		name : 'roleid'
	}])
}); 




function showRoleContent(record) {
	
    var cid=record.get('id');
    roleVersioninfoStore.load({
    	params:{
    		cid:cid
    	},
    	callback: function(r, options, success) { 
    		if (success == false) { 
    		alert("加载role-version数据失败，无对应数据或者系统出现异常！"); 
    		}
    		else{
    		//alert("加载domain-version数据成功！");	
    		}
    	}
    });
    rolepermissionsStore.load({ 
    	params : {
			cid : cid
		},
		callback: function(r, options, success) { 
		if (success == false) { 
		alert("加载数据失败，无对应数据或者系统出现异常！"); 
		}
		else{
			permissionStore = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
					url : 'showPermission.action?method=showPermission',
					method : 'post'
				}),
				reader : new Ext.data.JsonReader({
					root : "permission",
					totalProperty : "totalPermission",
					id:"id"
			    }, [ {
					name : 'id'
				}, {
					name : 'enName'
				},{
					name : 'cnName'
				},{
					name : 'description'
				},{
					name : 'permissionPackage'
				},{
					name : 'fileId'
				},{
					name : 'groupId'
				},{
					name : 'versionId'
				}])
			});
		permissionStore.load({
			params : {
				cid : cid
			},
			callback: function(r,options,success){
				if (success == false){
					alert("加载数据失败，无对应数据或者系统出现异常！");
				}
				else{
				var canvas=document.getElementById('roleCanvas');
				var ctx=canvas.getContext('2d');
				 drawRoleModel(350,200,ctx,rolepermissionsStore,permissionStore,record,canvas);
				 Ext.getDom('rolePermissionDetail').innerHTML = "";
				 var cm=new Ext.grid.ColumnModel([
				              					new Ext.grid.RowNumberer(),
				              					{header:'编号',dataIndex:'id'},
				              					{header:'名称(中文)',dataIndex:'cnName'},
				              					{header:'名称(英文)',dataIndex:'enName'},
				              					{header:'备注',dataIndex:'description'},
				              					{header:'权限包',dataIndex:'permissionPackage'}
				              				]);
				              			var grid=new Ext.grid.GridPanel({
				              					renderTo:'rolePermissionDetail',
				              					 viewConfig: {
				              			            forceFit:true
				              			        },
				              					layout:'fit',
				              					stripeRows: true,
				              					autoHeight:true,
				              					loadMask:true,
				              					store: permissionStore,
				              					cm:cm
				              					,			              						
				              					listeners : {
				              						cellclick : {
				              						fn : function(grid, rowIndex, columnIndex, e) {
				              							//点击记录，在表单中显示记录信息
				              						selected_row = rowIndex;
				              						var record = grid.getStore().getAt(rowIndex);
				              						var record2=permissionStore.getAt(rowIndex);
				              				    	//alert("perm:"+record2.get('id'));
				              				  	    showPermissionContent(record2);
				              				  	    Ext.getCmp('content-panel').addTab('perm-panel');
//				              						//var fAmount=grid.getCount();              				    
//				              						Ext.getCmp('content-panel').addTab('perm-panel');
//				              				      	showFunctionContent(functionStore.getAt(rowIndex).get('id'),functionStore.getAt(rowIndex).get('cnName'),processid,pText);
				              						},
				              						scope : this
				              					}
				              				}
				              				}); 
				}
			}
		});
			
			
			}
		}
		});
   }





function drawRoleModel(startx,starty,ctx,rolepermissionsStore,permissionStore,record,canvas){
	 ctx.restore();
	 ctx.clearRect(0, 0, 1500, 1500);
	 ctx.save();
	 ctx.translate(startx, starty);
	 
	 rpAmount=rolepermissionsStore.getCount();
	 pAmount=permissionStore.getCount();

	 var roleattr=new Array();
	 var permattr=new Array();

	 roleattr[0] = record.get('id');
	 roleattr[1] = record.get('enName');
	 roleattr[2] = record.get('description');
	 roleattr[3] = record.get('rolePackage');
	 roleattr[4] = record.get('parentRoleId');
	 roleattr[5] = record.get('roleType');
	 roleattr[6] = record.get('departmentid');
	 // roleattr[7] = record.get('fileId');
	 
	 var length=(roleattr.length+1)*20; 
	 var permlength;
	 var gradient = ctx.createLinearGradient(0, 0, 0, length);
	 gradient.addColorStop(0, 'rgb(252,158,22)');
	 gradient.addColorStop(1, 'rgb(254,243,153)');
	 ctx.fillStyle = gradient;
	 ctx.fillRect( 0, 0, 265, length); //渐变的背景色
	 
	 ctx.strokeStyle = '#000000'; 
	 ctx.beginPath(); 
	 ctx.rect(0.5,0.5, 265, length);
	 ctx.stroke();              //黑边框
	 
	 var roleImage=new Image();
	 roleImage.onload=function(){
	 ctx.drawImage(roleImage,3,2,16,16);
     };
	 roleImage.src='images/modelUnit/department/role.png';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "10pt 宋体";
	 ctx.fillText('角色:'+record.get('cnName'),25,16,265); // 标题栏
	 
	 ctx.beginPath();
	 ctx.moveTo(0,20);
	 ctx.lineTo(265,20);
	 ctx.stroke();   
	 ctx.beginPath();
	 ctx.moveTo(0.5,20.5);
	 ctx.lineTo(265.5,20.5);
	 ctx.stroke();        //画分割线
	 
	
	 
	 var roleattrImg=new Image();        //填写属性列表
	 roleattrImg.onload=function(){
		 
	 for (var i=0;i<roleattr.length;i++){
		
		 ctx.drawImage(roleattrImg,3,20+20*i,16,16);
	 };
		 
		 
	 	 
	};
	 roleattrImg.src='images/modelUnit/domainModel/attribute.gif';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "8pt Arial";
	 ctx.fillText('编号:'+roleattr[0],22,36+20*0);
	 ctx.fillText('英文名:'+roleattr[1],22,36+20*1,265);
	 ctx.fillText('详细信息:'+roleattr[2],22,36+20*2,265);
	 ctx.fillText('角色包:'+roleattr[3],22,36+20*3,265);
	 ctx.fillText('父角色:'+roleattr[4],22,36+20*4,265);
	 ctx.fillText('角色类型:'+roleattr[5],22,36+20*5,265);
	 ctx.fillText('所属部门:'+roleattr[6],22,36+20*6,265);
	 // ctx.fillText('版本:'+roleattr[7],22,36+20*7,265);//属性栏
	 
	 
	 ctx.beginPath();
	 ctx.lineWidth = 1;
	 ctx.moveTo(0,20+20*roleattr.length);
	 ctx.lineTo(265,20+20*roleattr.length);
	 ctx.stroke();   
	 ctx.beginPath();
	 ctx.moveTo(0.5,20+20*roleattr.length+0.5);
	 ctx.lineTo(265.5,20+20*roleattr.length+0.5);
	 ctx.stroke();  //画分割线
	 
/*	 var rolepermImg=new Image();         // 填写权限列表
     rolepermImg.onload=function(){ 
		 alert('pAmount='+permissionStore.getCount()+'rpAmount='+rpAmount);
     for (var j=0;j<rpAmount;j++){
		 ctx.drawImage(rolepermImg,3,(1+roleattr.length+j)*20,16,16);
		 ctx.fillStyle ="black";

		 perm=permissionStore.getAt(j);	
		 ctx.font = "10pt Arial";
		 ctx.fillText('权限'+(j+1)+': '+perm.get('cnName'),22,+(1+roleattr.length+j)*20+16,265); // 属性栏
	 }
     };
     rolepermImg.src='images/modelUnit/domainModel/state.gif';
*/	     
	 
		 var permImg=new Image();
		 permImg.onload=function(){
		var permattrImg=new Image();
	  		permattrImg.onload=function(){
	 for (var k=0;k<pAmount;k++)
	 {   
		 permattr[0] = permissionStore.getAt(k).get('id');
		 permattr[1] = permissionStore.getAt(k).get('enName');
		 permattr[2] = permissionStore.getAt(k).get('cnName');
		 permattr[3] = permissionStore.getAt(k).get('description');
		 permattr[4] = permissionStore.getAt(k).get('permissionPackage');
		 permattr[5] = permissionStore.getAt(k).get('fileId');
		 permattr[6] = permissionStore.getAt(k).get('groupId');
		 permattr[7] = permissionStore.getAt(k).get('versionId');	
		 permlength = (permattr.length+1)*20; 
		 var x=parseInt(-280*Math.cos(k*Math.PI*2/pAmount));
		     if (x>265) x=1.2*x;
		     var y=parseInt(-280*Math.sin(k*Math.PI*2/pAmount));
		     gradient = ctx.createLinearGradient(x, y, x, permlength);
			 gradient.addColorStop(0, 'rgb(205,205,205)');
			 gradient.addColorStop(1, 'rgb(255,255,255)');
			 ctx.fillStyle = gradient;
			 ctx.fillRect( x, y, 265, permlength); // 渐变的背景色
			
			 ctx.lineWidth = 1;
			 ctx.strokeStyle = '#000000'; 
			 ctx.beginPath(); 
			
			 ctx.rect(x-0.5,y-0.5, 265, permlength);
			 ctx.stroke();              // 黑边框
			 
			 
			
			 ctx.drawImage(permImg,x+3,y+2,16,16);
			
			 ctx.fillStyle ="black";
			 ctx.font = "10pt Arial";
			 ctx.fillText('权限:'+permattr[2],x+25,y+16,265); // 标题栏
			 
			 ctx.beginPath();
			 ctx.moveTo(x,y+20);
			 ctx.lineTo(x+265,y+20);
			 ctx.stroke();
			 
			 ctx.beginPath();
			 ctx.moveTo(x+0.5,y+20.5);
			 ctx.lineTo(x+265,y+20.5);
			 ctx.stroke();        // 画分割线
			 
			
				 for (var l=0;l<permattr.length-1;l++){
					 ctx.drawImage(permattrImg,x+3,y+22+l*20,16,16);
				 }
			
		 ctx.fillStyle ="black";
			 ctx.font = "8pt Arial";
	     ctx.fillText('编号:'+permattr[0],x+22,y+36);
		 ctx.fillText('英文名:'+permattr[1],x+22,y+56,265);
		 // ctx.fillText('详细信息:'+permattr[3],x+22,y+76,265);
		 ctx.fillText('权限包:'+permattr[4],x+22,y+76,265);
		 ctx.fillText('索引:'+permattr[5],x+22,y+96,265);
		 ctx.fillText('模型库:'+permattr[6],x+22,y+116,265);
		 ctx.fillText('版本信息:'+permattr[7],x+22,y+136,265);
		 ctx.fillText('详细信息:'+permattr[3],x+22,y+156,265);
		 drawLine(x+132.5,y+90,132.5,length/2,ctx,265,180);
	 }
	  		 };
	
		permattrImg.src='images/modelUnit/function/parameter_info.gif';
		canvas.onclick = onPermMouseClick;
	};
		permImg.src='images/modelUnit/department/perm.png';
			 
	
	}
function onPermMouseClick(event) {
	 //var pAmount=permissionStore.getCount();
	 var i=0;
	 var x=event.layerX-350;
	 var y=event.layerY-200;
	 if (x/1.2>265) x=x/1.2;
	 while (i<pAmount){
    if (x>-280*Math.cos(i*Math.PI*2/pAmount)
   	 && x<-280*Math.cos(i*Math.PI*2/pAmount)+265
        && y>-280*Math.sin(i*Math.PI*2/pAmount)
        && y<-280*Math.sin(i*Math.PI*2/pAmount)+180)
   {
   	
//   	 authenticate(functionStore.getAt(i).get('fileId'),'read',function(){
    	//alert("i="+i);
    	var record2=permissionStore.getAt(i);
    	//alert("perm:"+record2.get('id'));
  	    showPermissionContent(record2);
  	    Ext.getCmp('content-panel').addTab('perm-panel');
//     	 });
     	 break;
    }
     i++;
	 }
}  


